<template>
  <a-modal
    title="二维码"
    :visible="visible"
    :confirm-loading="loading"
    @ok="handleOk"
    @cancel="handleCancel"
    :afterClose="afterClose"
  >
    <a-form-model ref="form" :model="form" :rules="rules" :labelCol="{span:4}" :wrapperCol="{span:20}">
      <a-form-model-item has-feedback label="标题" prop="headline">
        <a-input v-model.trim="form.headline" autocomplete="off" :disabled="isEditor" />
      </a-form-model-item>
      <a-form-model-item has-feedback label="厂区" prop="factoryIds">
        <a-select v-model="form.factoryIds" default-value="" @focus="getFactoryData" mode="multiple">
          <a-select-option v-for="(item,index) in factorys" :key="index" :value="item.value">
            {{item.name}}
          </a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item has-feedback label="二维码" prop="qrCode">
        <a-upload
          accept="image/*"
          action=""
          list-type="picture-card"
          :file-list="fileList"
          :beforeUpload="beforeUpload"
          @change="handleChange"
          @preview='handlePreview'
          :disabled="isEditor"
        >
          <div v-if="fileList.length < 1">
            <a-icon type="plus" />
            <div class="ant-upload-text">
              上传
            </div>
          </div>
        </a-upload>
      </a-form-model-item>
      <a-form-model-item has-feedback label="类型" prop="codeType">
        <a-select v-model="form.codeType" :disabled="isEditor">
          <a-select-option v-for="(item,index) in codeTypes" :key="index" :value="item.value">
            {{item.name}}
          </a-select-option>
        </a-select>
      </a-form-model-item>
    </a-form-model>
  </a-modal>
  
</template>

<script>
import {uplaodFile,saveQRCode,fileUrl,getFactorysByName} from "../../api/index";
export default {
  name:"QRCodeFormModal",
  props:{
    visible:Boolean,
    formData:Object
  },
  computed:{
    isEditor(){
      return this.form.id?true:false;
    }
  },
  watch:{
    formData(newVal,oldVal){
      if(newVal&&newVal.id){
        this.form=newVal;
        this.form.factoryIds=newVal.factoryIds.split(',').map(item => item=parseInt(item));
        this.fileList=[{
          uid: '-1',
          name: 'image.png',
          status: 'done',
          url: fileUrl(this.form.qrCode),
        }];
        
      }
    }
  },
  data(){
    return {
      loading:false,
      fileList:[],
      factorys:[],
      form:{
        headline:"",
        codeType:"study",
        qrCode:"",
        factoryIds:[]
      },
      rules:{
        codeType:[{ required: true, message: '请选择类型', trigger: 'blur' },],
        qrCode:[{ required: true, message: '请上传二维码', trigger: 'blur' },],
        factoryId:[{ required: true, message: '请选择厂区', trigger: 'blur' },],
      },
      codeTypes:[
        {name:"学习墙",value:"study"},
        {name:"工作墙",value:"office"}
      ]
    }
  },
  created(){
    this.getFactoryData();
  },
  methods:{
    getFactoryData(){
      this.factorys=[]
      getFactorysByName().then(data =>{
        data.map(item =>{
          this.factorys.push({
            name:item.name,
            value:item.id
          })
        })
      })
    },
    afterClose(){
      this.$refs.form.resetFields();
      this.loading=false;
      this.$emit("update:formData",null);
      this.form={
        headline:"",
        codeType:"study",
        qrCode:"",
        factoryIds:[]
      };
      this.fileList=[];
    },
    handleCancel(){
      this.$emit("update:visible",false);
    },
    handleOk(){
      this.$refs.form.validate(valid =>{
        if(valid){ 
          this.loading=true;
          const data=Object.assign({},this.form);
          data.factoryId=data.factoryIds.join(',');
          saveQRCode(data).then(res =>{
            this.loading=false;
            this.$emit("update:visible",false);
            this.$emit("ok");
            this.$message.success(this.form.id?'编辑成功':'添加成功');
          }).catch(err =>{
            this.loading=false;
          })
        }
      })
    },
    beforeUpload(file,fileList){
      //console.log("upload:",file,fileList);
      uplaodFile(file).then(data =>{
        this.form.qrCode=data;
      });
      return false;
    },
    handleChange({ fileList }) {
      this.fileList = fileList;
    },
    handlePreview(file){
      window.open(`${fileUrl(this.form.qrCode)}`,'_target');
    }
  }
}
</script>

<style lang="scss" scoped>

</style>